'''
Created on Sep 8, 2012

@author: petrina
'''

from link_indicator import Link_indicator
from indiutils import Indiutils
from indicator import * 
import logging

class Planning_speed_index(Link_indicator):
    '''
    Calculates the Planning-Speed-Index for every time interval (using aggregated fcd-data).
    
    Formula for calculating the indicator: 
        psi_i = wts_i/fts_i
            where:
                - psi_i ... planning speed index at time interval i
                - wts_i...  worst case travel speed at time interval i
                - fts ... free flow travel speed
    
    The 5th percentile travel speed acts as worst case travel speed (@see processing.planning_speed)
    
    unit: percent (%)
    '''


    def __init__(self, link):
        '''        
        @param link: Link-object (containing valid records and timeseries) 
        '''
        
        Link_indicator.__init__(self, link)   
        self._name = 'Planning Speed Index'
        self._mr_name =  'plan_speed_idx'
        self._unit = '%'
        #self.unit.__doc__ = 'percent (%)'
        
        self._create_title(link.id, link.direction)
        
        logging.debug("Planning_speed_index: created new Planning_speed_index")
        
        
    #----------------------public methods of the class-------------------------


    def calculate(self):
        '''
        @return: planning speed index for every time interval as list of Result_values
        '''
        
        logging.info("Planning_speed_index: staring to calculate the planning speed index")
        
        utils = Indiutils()
        pts = utils.p5_speed(self._link.timeseries) 
        
        self._values = []
        for i in range(len(pts)):
            psi = None if pts[i] is None else pts[i]/self._link.freespeed
            self._values.append(psi)
        
        logging.debug("Planning_speed_index: finished calculating the planning speed index")

        return self._values